#include <map>
#include <vector>
#include <string>
#include "smartpipe.h"

using namespace std;
using namespace sp;

void registe(){
    Image::Crop::cropWithInput::registe();
    Image::Crop::cropWithParas::registe();
    Image::Gen::genFromDisk::registe();
    Image::Gen::genFromMemory::registe();
    Image::Mark::markWithString::registe();
    Image::Pull::pullFromRtsp::registe();
    Image::Push::pushWithRtsp::registe();
    Image::Push::pushWithRtmp::registe();
    Image::Resize::letterBoxResize::registe();
    Image::Resize::resize::registe();
    Image::Save::markAndSave::registe();
    Image::Save::save::registe();
    Image::Trans::trans::registe();
    Model::Group::groupByBatch::registe();
    Model::Split::splitByShape::registe();
    Model::Trans::transferToDeviceMemory::registe();
    Model::Trans::transferToHostMemory::registe();
    Model::Yolo::yolo_complete::registe();
    Model::Yolo::yolo_preprocess::registe();
    Model::Yolo::yolo_inference::registe();
    Model::Yolo::yolo_postprocess::registe();
    Model::Retinanet::retinanet_preprocess::registe();
    Model::Retinanet::retinanet_inference::registe();
    Model::Retinanet::retinanet_postprocess::registe();
    Model::LPRnet::lprnet_preprocess::registe();
    Model::LPRnet::lprnet_inference::registe();
    Model::LPRnet::lprnet_postprocess::registe();
    Model::Openpose::openpose_preprocess::registe();
    Model::Openpose::openpose_inference::registe();
    Model::Openpose::openpose_postprocess::registe();
    Tool::Group::groupByRequestId::registe();
    Tool::Split::splitByFlowId::registe();
}

char* DATA_SOURCE;
char* DATA_TARGET;

char* input_video_data_ptr;
char* output_video_data_ptr;

// 共用参数
string video_path = "/data/lx/SmartPipe/data_source/videos/0123.mp4";                   // 视频路径
string save_path = "/data/lx/SmartPipe/apps/car_license_plate_recognition/output.avi";  // 输出路径
int video_channels = 1;         // 视频路数
long cnt = 30*540;              // 单路视频帧数
int fps = 30;                   // 单路视频帧率

// 单路视频 在线运行 指定部署方式
void app_run_online(SharedMemoryManager& smm, Gpu_SharedMemoryManager& gpu_smm){
    // 构造app
    Function* f0 = new Image::Pull::pullFromRtsp("rtsp://202.38.75.242:554/live/0", 3840, 2160, cnt);
    Function* f1 = new Image::Resize::resize(640, 384);
    Function* f2 = new Model::Yolo::yolo_preprocess(640, 384);
    Function* f3 = new Model::Group::groupByBatch(1);
    Function* f4 = new Model::Trans::transferToDeviceMemory();
    Function* f5 = new Model::Yolo::yolo_inference(640, 384);
    Function* f6 = new Model::Trans::transferToHostMemory();
    Function* f7 = new Model::Split::splitByShape();
    Function* f8 = new Model::Yolo::yolo_postprocess(640, 384);
    Function* f9 = new Image::Crop::cropWithInput(0);
    Function* f10 = new Image::Resize::resize(320, 320);
    Function* f11 = new Model::Retinanet::retinanet_preprocess();
    Function* f12 = new Model::Group::groupByBatch(1);
    Function* f13 = new Model::Trans::transferToDeviceMemory();
    Function* f14 = new Model::Retinanet::retinanet_inference();
    Function* f15 = new Model::Trans::transferToHostMemory();
    Function* f16 = new Model::Split::splitByShape();
    Function* f17 = new Model::Retinanet::retinanet_postprocess();
    Function* f18 = new Image::Crop::cropWithInput(1);
    Function* f19 = new Image::Resize::resize(94, 24);
    Function* f20 = new Model::LPRnet::lprnet_preprocess();
    Function* f21 = new Model::Group::groupByBatch(1);
    Function* f22 = new Model::Trans::transferToDeviceMemory();
    Function* f23 = new Model::LPRnet::lprnet_inference();
    Function* f24 = new Model::Trans::transferToHostMemory();
    Function* f25 = new Model::Split::splitByShape();
    Function* f26 = new Model::LPRnet::lprnet_postprocess();
    Function* f27 = new Tool::Group::groupByRequestId(cnt);
    Function* f28 = new Image::Mark::markWithString();
    Function* f29 = new Image::Resize::resize(1920, 1080);
    Function* f30 = new Image::Push::pushWithRtsp("rtsp://202.38.75.242:554/live/1", fps, 1920, 1080);
    // 连接逻辑关系
    connectOneToMany(3, f0, f9, f28);
    connect(f10, f18);
    connectOneByOne(31, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, f21, f22, f23, f24, f25, f26, f27, f28, f29, f30);
    // 部署表
    map<short, vector<Function*>> Fs_map; // Fs_map Function映射到CPU逻辑核（Excutor)
    map<short, vector<int>> Gpus_map;     // Gpus_map GPU映射到CPU逻辑核
    Fs_map[0] = {f0};
    Fs_map[1] = {f1};
    Fs_map[2] = {f2};
    Fs_map[3] = {f3};
    Fs_map[4] = {f4, f6, f13, f15, f22, f24};
    Fs_map[5] = {f5, f14, f23};
    Fs_map[6] = {f7};
    Fs_map[7] = {f8};
    Fs_map[8] = {f9};
    Fs_map[9] = {f10};
    Fs_map[10] = {f11};
    Fs_map[11] = {f12};
    Fs_map[12] = {f16};
    Fs_map[13] = {f17};
    Fs_map[14] = {f18};
    Fs_map[15] = {f19};
    Fs_map[16] = {f20};
    Fs_map[17] = {f21};
    Fs_map[18] = {f25};
    Fs_map[19] = {f26};
    Fs_map[20] = {f27};
    Fs_map[21] = {f28};
    Fs_map[22] = {f29};
    Fs_map[23] = {f30};
    Gpus_map[0] = {4, 5};
    // 构造app
    App app(0, Fs_map, Gpus_map, &smm, &gpu_smm); // 构造App, app_id, Fs_map, Gpus_map, smm, gpu_smm
    // 运行app
    app.check();
    app.printMsg();
    app.init();
    app.run();
    app.waitForComplete();
    // 检查是否存在内存泄露
    smm.check();
}

// 单路视频 离线运行 指定部署方式
void app_run_offline(SharedMemoryManager& smm, Gpu_SharedMemoryManager& gpu_smm){
    // 将视频加载到内存中
    SharedMemoryPool input_video_pool = smm.createSharedMemoryPool("input_video", Cv_Mat_Data_3840_2160_Memory, cnt);
    SharedMemoryPool output_video_pool = smm.createSharedMemoryPool("output_video", Cv_Mat_Data_3840_2160_Memory, cnt);
    input_video_data_ptr = input_video_pool.getDataPtr();
    output_video_data_ptr = output_video_pool.getDataPtr();
    Image::Gen::genFromDisk f_start(video_path, input_video_data_ptr, cnt, 30, 3840, 2160);
    f_start.run();
    // 构造app
    Function* f0 = new Image::Gen::genFromMemory(input_video_data_ptr, cnt, fps, 3840, 2160);
    Function* f1 = new Image::Resize::resize(640, 384);
    Function* f2 = new Model::Yolo::yolo_preprocess(640, 384);
    Function* f3 = new Model::Group::groupByBatch(1);
    Function* f4 = new Model::Trans::transferToDeviceMemory();
    Function* f5 = new Model::Yolo::yolo_inference(640, 384);
    Function* f6 = new Model::Trans::transferToHostMemory();
    Function* f7 = new Model::Split::splitByShape();
    Function* f8 = new Model::Yolo::yolo_postprocess(640, 384);
    Function* f9 = new Image::Crop::cropWithInput(0);
    Function* f10 = new Image::Resize::resize(320, 320);
    Function* f11 = new Model::Retinanet::retinanet_preprocess();
    Function* f12 = new Model::Group::groupByBatch(1);
    Function* f13 = new Model::Trans::transferToDeviceMemory();
    Function* f14 = new Model::Retinanet::retinanet_inference();
    Function* f15 = new Model::Trans::transferToHostMemory();
    Function* f16 = new Model::Split::splitByShape();
    Function* f17 = new Model::Retinanet::retinanet_postprocess();
    Function* f18 = new Image::Crop::cropWithInput(1);
    Function* f19 = new Image::Resize::resize(94, 24);
    Function* f20 = new Model::LPRnet::lprnet_preprocess();
    Function* f21 = new Model::Group::groupByBatch(1);
    Function* f22 = new Model::Trans::transferToDeviceMemory();
    Function* f23 = new Model::LPRnet::lprnet_inference();
    Function* f24 = new Model::Trans::transferToHostMemory();
    Function* f25 = new Model::Split::splitByShape();
    Function* f26 = new Model::LPRnet::lprnet_postprocess();
    Function* f27 = new Tool::Group::groupByRequestId(cnt);
    Function* f28 = new Image::Mark::markWithString();
    Function* f29 = new Image::Save::save(save_path, output_video_data_ptr, cnt, 30, 3840, 2160);
    // 连接逻辑关系
    connectOneToMany(3, f0, f9, f28);
    connect(f10, f18);
    connectOneByOne(30, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17, f18, f19, f20, f21, f22, f23, f24, f25, f26, f27, f28, f29);
    // 部署表
    map<short, vector<Function*>> Fs_map; // Fs_map Function映射到CPU逻辑核（Excutor)
    map<short, vector<int>> Gpus_map;     // Gpus_map GPU映射到CPU逻辑核
    Fs_map[0] = {f0};
    Fs_map[1] = {f1};
    Fs_map[2] = {f2};
    Fs_map[3] = {f3};
    Fs_map[4] = {f4, f6, f13, f15, f22, f24};
    Fs_map[5] = {f5, f14, f23};
    Fs_map[6] = {f7};
    Fs_map[7] = {f8};
    Fs_map[8] = {f9};
    Fs_map[9] = {f10};
    Fs_map[10] = {f11};
    Fs_map[11] = {f12};
    Fs_map[12] = {f16};
    Fs_map[13] = {f17};
    Fs_map[14] = {f18};
    Fs_map[15] = {f19};
    Fs_map[16] = {f20};
    Fs_map[17] = {f21};
    Fs_map[18] = {f25};
    Fs_map[19] = {f26};
    Fs_map[20] = {f27};
    Fs_map[21] = {f28};
    Fs_map[22] = {f29};
    Gpus_map[0] = {4, 5};
    // 构造app
    App app(0, Fs_map, Gpus_map, &smm, &gpu_smm); // 构造App, app_id, Fs_map, Gpus_map, smm, gpu_smm
    // 运行app
    app.check();
    app.printMsg();
    app.init();
    app.run();
    app.waitForComplete();
    // 检查是否存在内存泄露
    smm.check();
    // 将输出视频保存到磁盘
    Image::Save::save f_end(save_path, output_video_data_ptr, cnt, 30, 3840, 2160);
    f_end.run();
}

// 多路视频 在线运行 评测并部署 (profile and deploy)
void app_run_multiFlow_online_PAD(SharedMemoryManager& smm, Gpu_SharedMemoryManager& gpu_smm){
    // TODO: 待完成
}

int main(){
    // 注册
    registe();
    // 声明共享内存管理对象
    SharedMemoryManager smm;
    Gpu_SharedMemoryManager gpu_smm;
    // 单路视频离线测试
    // app_run_offline(smm, gpu_smm);
    // 单路视频在线测试
    app_run_online(smm, gpu_smm);
    // 多路视频在线测试，评测部署的方式运行
    // app_run_multiFlow_online_PAD(smm, gpu_smm);
}